
var libPath = 'lib';
var atlantaLibPath = 'web/skin/default/libs/atlanta/js';

var publish = 'wb';

var gulp = require( 'gulp');
var ngAnnotate = require('gulp-ng-annotate');
var uglify = require( 'gulp-uglify');
var concat = require( 'gulp-concat');
var minifyCss = require( 'gulp-minify-css');
var rename = require( 'gulp-rename');
var ngmin = require( 'gulp-ngmin');
var stripDebug = require( 'gulp-strip-debug');
var htmlmin = require( 'gulp-htmlmin');
var jshint = require( 'gulp-jshint');
var changed = require( 'gulp-changed');

var args = {
    minJs:false
};

var tasksArgs = {
    minifyWeb: {
        taskName: 'minifyWeb',
        src: ['web/modules/start/module.js', 'web/modules/start/app.js'],
        dest: 'www/js/',
        fileName: 'app.js'
    },
    minifyModuleJs: {
        taskName: 'minifyModuleJs',
        src: ['web/modules/**/*.js', '!web/modules/start/*.js', 'web/publish/'+ publish +'/CambioConfig.js'],
        dest: 'www/js/',
        fileName: 'modules.js'
    },
    minifycss: {
        taskName: 'minifycss',
        src: [
            'node_modules/ng-dialog/css/ngDialog.css',
            'node_modules/ng-dialog/css/ngDialog-theme-default.css',
            'lib/sweet-alert/sweetalert.css',
            'web/skin/default/css/*.css',
            'web/modules/**/csses/*.css'
        ],
        dest: 'www/skin/default/css/',
        fileName: 'final.css'
    },
    moveCsslibs: {
        taskName: 'moveCsslibs',
        src: ['web/skin/default/libs/**'],
        dest: 'www/skin/default/libs/'
    },
    moreTemplatelibs: {
        taskName: 'moreTemplatelibs',
        src: ['web/skin/template/**'],
        dest: 'www/skin/template/'
    },
    moveImg: {
        taskName: 'moveImg',
        src: ['web/skin/default/img/*.*'],
        dest: 'www/skin/default/img/'
    },
    minifyLib: {
        taskName: 'minifyLib',
        src: [
            'node_modules'+'/angular/angular.min.js',
            'node_modules'+'/angular-route/angular-route.min.js',
            'node_modules'+'/angular-ui-router/release/angular-ui-router.min.js',
            'node_modules'+'/angular-resource/angular-resource.min.js',
            'node_modules'+'/angular-animate/angular-animate.min.js',
            'node_modules'+'/ng-dialog/js/ngDialog.min.js',
            'node_modules'+'/ng-file-upload/dist/ng-file-upload-shim.js',
            'node_modules'+'/ng-file-upload/dist/ng-file-upload.js',
            'node_modules'+'/angular-translate/dist/angular-translate.min.js',
            'node_modules'+'/angular-translate-loader-static-files/angular-translate-loader-static-files.min.js',
            'node_modules'+'/echarts/dist/echarts.min.js',
            'node_modules'+'/js-base64/base64.js',
            'node_modules'+'/angular-ui-tree/dist/angular-ui-tree.js',

            atlantaLibPath+'/plugins/jquery/jquery.min.js',
            atlantaLibPath+'/plugins/jquery/jquery.combo.select.js',
            atlantaLibPath+'/plugins/jquery/jquery-ui.min.js',
            atlantaLibPath+'/plugins/jquery-table2excel-master/dist/jquery.table2excel.js',

            atlantaLibPath+'/plugins/bootstrap/bootstrap.min.js',

            libPath+'/sweet-alert/sweetalert.min.js',
            libPath+'/qrcode.js/jquery.qrcode.min.js',
            libPath+'/js-xlsx/xlsx.core.min.js',
            libPath+'/js-xlsx/angular-js-xlsx.js',
            libPath+'/FileSaver/FileSaver.js',
            // libPath+'/dist/summernote.js',

            atlantaLibPath+'/echarts/mb.js',

            atlantaLibPath+'/plugins/icheck/icheck.min.js',
            atlantaLibPath+'/plugins/mcustomscrollbar/jquery.mCustomScrollbar.min.js',
            atlantaLibPath+'/plugins/codemirror/codemirror.js',
            atlantaLibPath+'/plugins/codemirror/mode/htmlmixed/htmlmixed.js',
            atlantaLibPath+'/plugins/codemirror/mode/xml/xml.js',
            atlantaLibPath+'/plugins/codemirror/mode/javascript/javascript.js',
            atlantaLibPath+'/plugins/codemirror/mode/css/css.js',
            atlantaLibPath+'/plugins/codemirror/mode/clike/clike.js',
            atlantaLibPath+'/plugins/codemirror/mode/php/php.js',
            atlantaLibPath+'/plugins/summernote/summernote.js',

            atlantaLibPath+'/plugins/scrolltotop/scrolltopcontrol.js',
            atlantaLibPath+'/plugins/morris/raphael-min.js',
            atlantaLibPath+'/plugins/knob/jquery.knob.min.js',
            atlantaLibPath+'/plugins/morris/morris.js',
            atlantaLibPath+'/plugins/rickshaw/d3.v3.js',
            atlantaLibPath+'/plugins/nvd3/nv.d3.min.js',
            atlantaLibPath+'/plugins/rickshaw/rickshaw.min.js',
            atlantaLibPath+'/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js',
            atlantaLibPath+'/plugins/jvectormap/jquery-jvectormap-world-mill-en.js',
            atlantaLibPath+'/plugins/bootstrap/bootstrap-datepicker.js',
            atlantaLibPath+'/plugins/owl/owl.carousel.min.js',
            atlantaLibPath+'/plugins/moment.min.js',
            atlantaLibPath+'/plugins/daterangepicker/daterangepicker.js',

            atlantaLibPath+'/plugins/datatables/jquery.dataTables.min.js',
            atlantaLibPath+'/plugins/tableexport/tableExport.js',
            atlantaLibPath+'/plugins/tableexport/jquery.base64.js',
            atlantaLibPath+'/plugins/tableexport/html2canvas.js',
            // atlantaLibPath+'/plugins/tableexport/jspdf/libs/sprintf.js',
            // atlantaLibPath+'/plugins/tableexport/jspdf/jspdf.js',
            // atlantaLibPath+'/plugins/tableexport/jspdf/libs/base64.js',


            atlantaLibPath+'/plugins/bootstrap/bootstrap-datepicker.js',
            atlantaLibPath+'/plugins/bootstrap/bootstrap-file-input.js',
            atlantaLibPath+'/plugins/bootstrap/bootstrap-select.js',
            atlantaLibPath+'/plugins/tagsinput/jquery.tagsinput.min.js',
            atlantaLibPath+'/plugins.js',
            atlantaLibPath+'/actions.js',
            atlantaLibPath+'/demo_dashboard.js'
        ],
        dest: 'www/lib/',
        fileName: 'libs.js'
    },
    minifyTemplates: {
        taskName: 'minifyTemplates',
        src: ['web/modules/**/templates/**/*.html'],
        dest: 'www/templates/'
    },
    moveIndexHtml: {
        taskName: 'moveIndexHtml',
        src: ['web/publish/index.html'],
        dest: 'www/'
    },
    moveFiles: {
        taskName: 'moveFiles',
        src: ['web/files/**'],
        dest: 'www/lib/'
    }
};

var taskNames = [];

for(var key in tasksArgs){
    var t = tasksArgs[key];
    taskNames.push(t.taskName);
    var doThings = [];
    if(t.isJsHint){
        doThings.push('jshint');
    }
    doThings.push(t.taskName);
    // gulp.watch(t.src, doThings);
}

gulp.task('default',taskNames);

gulp.task('jshint', function () {
    return gulp.src('web/js/**')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

gulp.task(tasksArgs.minifyWeb.taskName, function() {
    if(args.minJs){
        return gulp.src(tasksArgs.minifyWeb.src)
            .pipe(stripDebug())
            .pipe(ngAnnotate())
            .pipe(concat(tasksArgs.minifyWeb.fileName))
            .pipe(ngmin({dynamic: false}))
            .pipe(rename({suffix: '.min'}))
            .pipe(uglify({outSourceMap: false}))
            .pipe(gulp.dest(tasksArgs.minifyWeb.dest))
    }else{
        return gulp.src(tasksArgs.minifyWeb.src)
            .pipe(stripDebug())
            .pipe(ngAnnotate())
            .pipe(concat(tasksArgs.minifyWeb.fileName))
            .pipe(rename({suffix: '.min'}))
            .pipe(gulp.dest(tasksArgs.minifyWeb.dest))
    }
});

gulp.task(tasksArgs.minifyModuleJs.taskName, function() {
    if(args.minJs){
        return gulp.src(tasksArgs.minifyModuleJs.src)
            .pipe(stripDebug())
            .pipe(ngAnnotate())
            .pipe(concat(tasksArgs.minifyModuleJs.fileName))
            .pipe(ngmin({dynamic: false}))
            .pipe(rename({suffix: '.min'}))
            .pipe(uglify({outSourceMap: false}))
            .pipe(gulp.dest(tasksArgs.minifyModuleJs.dest))
    }else{
        return gulp.src(tasksArgs.minifyModuleJs.src)
            .pipe(stripDebug())
            .pipe(ngAnnotate())
            .pipe(concat(tasksArgs.minifyModuleJs.fileName))
            .pipe(rename({suffix: '.min'}))
            .pipe(gulp.dest(tasksArgs.minifyModuleJs.dest))

    }
});

gulp.task(tasksArgs.minifycss.taskName, function() {
    return gulp.src(tasksArgs.minifycss.src)      //压缩的文件
        .pipe(concat(tasksArgs.minifycss.fileName))
        .pipe(gulp.dest(tasksArgs.minifycss.dest))
        .pipe(minifyCss())   //执行压缩
        .pipe(gulp.dest(tasksArgs.minifycss.dest))
});

gulp.task(tasksArgs.moveCsslibs.taskName, function() {
    return gulp.src(tasksArgs.moveCsslibs.src)
        .pipe(changed(tasksArgs.moveCsslibs.dest))
        .pipe(gulp.dest(tasksArgs.moveCsslibs.dest))
});

gulp.task(tasksArgs.moreTemplatelibs.taskName, function() {
    return gulp.src(tasksArgs.moreTemplatelibs.src)
        .pipe(changed(tasksArgs.moreTemplatelibs.dest))
        .pipe(gulp.dest(tasksArgs.moreTemplatelibs.dest))
});

gulp.task(tasksArgs.moveImg.taskName, function() {
    return gulp.src(tasksArgs.moveImg.src)
        .pipe(changed(tasksArgs.moveImg.dest))
        .pipe(gulp.dest(tasksArgs.moveImg.dest))
});

gulp.task(tasksArgs.minifyLib.taskName, function() {
    return gulp.src(tasksArgs.minifyLib.src)
        .pipe(stripDebug())
        .pipe(ngAnnotate())
        .pipe(concat(tasksArgs.minifyLib.fileName))
        //.pipe(gulp.dest(tasksArgs.minifyLib.dest))
        //.pipe(ngmin({dynamic: false}))
        //.pipe(uglify({outSourceMap: false}))
        .pipe(gulp.dest(tasksArgs.minifyLib.dest))
});

gulp.task(tasksArgs.minifyTemplates.taskName, function() {
    return gulp.src(tasksArgs.minifyTemplates.src)
        .pipe(changed(tasksArgs.minifyTemplates.dest))
        .pipe(htmlmin({collapseWhitespace: true,removeComments: true}))
        .pipe(rename({dirname: ''}))
        .pipe(gulp.dest(tasksArgs.minifyTemplates.dest))
});

gulp.task(tasksArgs.moveIndexHtml.taskName, function(){
    return gulp.src(tasksArgs.moveIndexHtml.src)
        .pipe(changed(tasksArgs.moveIndexHtml.dest))
        .pipe(gulp.dest(tasksArgs.moveIndexHtml.dest))
});

gulp.task(tasksArgs.moveFiles.taskName, function(){
    return gulp.src(tasksArgs.moveFiles.src)
        .pipe(changed(tasksArgs.moveFiles.dest))
        .pipe(gulp.dest(tasksArgs.moveFiles.dest))
});
